﻿#region summary

// Copyright (c)  YISH. All rights reserved.
// Licensed under the MIT License, See License.txt in the project root for license information.

#endregion

using System;

namespace NSQL.Expressions
{
    public class ComparisonExpression : BaseExpression, IComparisonExpression
    {
        public IExpression Left { get; set; }

        public IExpression Right { get; set; }

        public ComparisonOperator Operator { get; set; }

        public ComparisonExpression(IExpression left, IExpression right,
            ComparisonOperator op)
        {
            Left = left;
            Right = right;
            Operator = op;
        }

        public static ComparisonOperator ParseOperator(string op)
        {
            switch (op)
            {
                case "=": return ComparisonOperator.Equal;
                case "<>":
                case "^=":
                case "~=":
                case "!=": return ComparisonOperator.NotEqual;
                case ">": return ComparisonOperator.Greater;
                case "<": return ComparisonOperator.Less;
                case ">=": return ComparisonOperator.GreaterOrEqual;
                case "<=": return ComparisonOperator.LessOrEqual;
                default:
                    throw new Exception("UnExpected relational operator.expecting {=, <>, ^=, ~=, !=, >, <, >=, <=}");
            }
        }

        public override T Accept<T>(IExpressionVisitor<T> visitor) => visitor.Visit(this);
        public override void Accept(IExpressionVisitor visitor) => visitor.Visit(this);
        public override bool IsValid => Left.IsValid && Right.IsValid;
        protected override IExpression DeepCopy()
        {
            return new ComparisonExpression(Left, Right, Operator)
            {
                Tag = Tag
            };
        }
    }
}